/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package radio.sessions;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import radio.entities.AccountDetails;
import radio.entities.Accounts;
import radio.entities.AccountsType;
import radio.entities.City;
import radio.entities.PaymentDetails;
import radio.entities.PaymentDetailsPK;

/**
 *
 * @author Huu Vinh
 */
@Stateless
public class AccountsFacade extends AbstractFacade<Accounts> {

    @EJB
    private PaymentDetailsFacade paymentDetailsFacade;
    @Resource
    SessionContext context;
    @EJB
    private AccountDetailsFacade accountDetailsFacade;
    @EJB
    private CityFacade cityFacade;
    @EJB
    private AccountsTypeFacade accountsTypeFacade;
    @PersistenceContext(unitName = "RadioCabsEJB-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public AccountsFacade() {
        super(Accounts.class);
    }

    public Accounts login(String Uid, String Pwd) {
        List<Accounts> result = em.createNamedQuery("Accounts.login").setParameter("uid", Uid).setParameter("pwd", Pwd).getResultList();
        if (!result.isEmpty()) {
            return result.get(0);
        } else {
            return null;
        }
    }

    public Accounts findbyId(int accId) {
        List<Accounts> result = em.createNamedQuery("Accounts.findByAccId").setParameter("accId", accId).getResultList();
        if (!result.isEmpty()) {
            return result.get(0);
        } else {
            return null;
        }
    }

    public Accounts findbyUsername(String userid) {
        return (Accounts) em.createNamedQuery("Accounts.findByUid").setParameter("uid", userid).getResultList().get(0);
    }

    public boolean confirmPayment(String uid, String typeName, PaymentDetailsPK paymentDetailId) {

        Calendar c = GregorianCalendar.getInstance(Locale.getDefault());
        Accounts change = findbyUsername(uid);
        PaymentDetails pmdt = paymentDetailsFacade.find(paymentDetailId);
        try {
            int noDate = pmdt.getPaymentRate().getNoDate();
            if (change.getStatus()) {
                change.setTypeId(accountsTypeFacade.getId(typeName));
                change.setStartDate(c.getTime());
                if (change.getEndDate() != null) {
                    if (change.getEndDate().after(c.getTime())) {
                        c.setTime(change.getEndDate());
                        c.add(Calendar.DAY_OF_MONTH, noDate);
                        change.setEndDate(c.getTime());
                    } else {
                        c.add(Calendar.DAY_OF_MONTH, noDate);
                        change.setEndDate(c.getTime());
                    }
                } else {
                    c.add(Calendar.DAY_OF_MONTH, noDate);
                    change.setEndDate(c.getTime());
                }

                pmdt.setStatus(true);
                edit(change);
                paymentDetailsFacade.edit(pmdt);
                return true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean insertnewAcc(String uid, String pwd, boolean gender, int cityid, String contact, String mobile, String tele, String address, String email) {
        try {
            AccountsType type = accountsTypeFacade.getId("User");
            Accounts acc = new Accounts(1, uid, pwd, gender, new Date(), null, true, type);

<<<<<<< .mine
            
            AccountsType type=accountsTypeFacade.getId("User");                
        Accounts acc=new Accounts(1, uid, pwd, gender, new Date(),null, true, type);
        
        this.create(acc);
        Accounts temp=this.findbyUsername(uid);
        AccountDetails detail=new AccountDetails(temp.getAccId(), contact, mobile, tele, address, email);
        City city=cityFacade.find(cityid);        
        detail.setAddress(address);
        detail.setCityId(city);
        detail.setContactPerson(contact);
        detail.setEmail(email);
        detail.setMobile(mobile);
        detail.setTelephone(tele);                    
        accountDetailsFacade.create(detail);
        temp.setAccountDetails(detail);
        return true;
        }catch(Exception ex){
=======
            this.create(acc);
            Accounts temp = this.findbyUsername(uid);
            AccountDetails detail = new AccountDetails(temp.getAccId(), contact, mobile, tele, address, email);
            City city = cityFacade.findbyId(cityid);
            detail.setAddress(address);
            detail.setCityId(city);
            detail.setContactPerson(contact);
            detail.setEmail(email);
            detail.setMobile(mobile);
            detail.setTelephone(tele);

            accountDetailsFacade.create(detail);
            return true;
        } catch (Exception ex) {
>>>>>>> .r54
            context.setRollbackOnly();
            ex.printStackTrace();
            return false;
        } finally {
        }
    }
<<<<<<< .mine
    public List<Accounts> SearchDriver(int cityid){
        AccountsType type=accountsTypeFacade.getId("Driver");
        City cit=cityFacade.find(cityid);
        List<Accounts> result = em.createNamedQuery("Accounts.searchDriver").setParameter("cityId", cit).setParameter("typeId", type).getResultList();
        if (!result.isEmpty()) {
            return result;
        } else {
            return null;
        }
    }
    
=======
>>>>>>> .r54
}
